package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.College;
import com.exam.demo.entity.Paper;
import com.exam.demo.entity.Question;
import com.exam.demo.entity.Questions;
import com.exam.demo.mapper.PaperMapper;
import com.exam.demo.mapper.QuestionMapper;
import com.exam.demo.mapper.QuestionsMapper;
import com.exam.demo.service.PaperService;
import com.exam.demo.service.QuestionService;
import com.exam.demo.service.QuestionsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/paper")
public class PaperController {
    @Resource
    PaperService paperService;
    @Resource
    PaperMapper paperMapper;

    @Resource
    QuestionsService questionsService;
    @Resource
    QuestionService questionService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String paperName,
                           @RequestParam(defaultValue = "0") String teacherId
                           ) {
        Page<Paper> page = paperService.findPage(new Page<Paper>(pageNum, pageSize), paperName,teacherId);
        List<Paper> paperList = page.getRecords();
        for (Paper paper :
                paperList) {
            //根据paper_id -> question_id ->question ->questionList
            QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("paper_id", paper.getPaperId());
            List<Questions> list = questionsService.list(queryWrapper);
            List<Integer> questionIdList = list.stream().map(Questions::getQuestionId).collect(Collectors.toList());
            if (questionIdList.size() > 0) { //试卷有试题才添加
                List<Question> questionList = questionService.listByIds(questionIdList);
                paper.setQuestionList((ArrayList) questionList);
                System.out.println(paperList);
                page.setRecords(paperList);
            }
        }
        return Result.success(page);
    }

    //获取一张试卷
    @PostMapping("/findPaperByPaperId")
    public Result findPaperByPaperId(@RequestBody Paper paper) {
        paper = paperService.getById(paper.getPaperId());
        //根据paper_id -> question_id ->question ->questionList
        QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_id",paper.getPaperId());
        List<Questions> list = questionsService.list(queryWrapper);
        List<Integer> questionIdList = list.stream().map(Questions::getQuestionId).collect(Collectors.toList());
        if (questionIdList.size() > 0) { //试卷有试题才添加
            List<Question> questionList = questionService.listByIds(questionIdList);
            paper.setQuestionList((ArrayList) questionList);
        }
        return Result.success(paper);
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Paper paper) {
        ArrayList<Question> questionList = paper.getQuestionList();
        Integer paperId = paper.getPaperId();
        int insert;
        if (paperId == null) {
            if (paperMapper.insert(paper) > 0) {
                paperId = paper.getPaperId();
            }
        } else {
            paperService.saveOrUpdate(paper);
        }
        //先清空，存入试卷对应题目
        QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_id", paperId);
        questionsService.remove(queryWrapper);
        for (Question question :
                questionList) {
            questionsService.save(new Questions(null, paperId, "", question.getQuestionId()));
        }
        //存入试卷基本信息
        return Result.success(true);
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(paperService.removeById(id));
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        response = paperService.export(response);
    }

    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        boolean b = paperService.imp(file);
        return Result.success(b); //false导入失败
    }




}
